import { IncomingMessage } from "http";
import { Logger } from "./Logger";

export class WebUtil {

    public static getIpByExpressReq(req: IncomingMessage): string {
        // Logger.log("getIpByExpressReq=remoteAddress=", req.socket.remoteAddress)
        let realIp: string | string[] = req.headers["X-Real-IP"]
        let ip: string = ""
        if (realIp) {
            if (typeof (realIp) == "string") {
                ip = realIp
            }
        }
        ip = ip || req.socket.remoteAddress
        if (ip && ip.indexOf('::ffff:') != -1) {
            ip = ip.substring(7);
        }
        // Logger.log("getIpByExpressReq=ip=", ip)
        return ip;
    }
}